/**
 * 数据字典 - 编辑页面
 * @author yuhuan
 * @date 2018/12/20 10:35
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            detail: false,
            form: '',
            rules: {
                TYPE: [
                    {required: true, message: '所属分组不能为空', trigger: 'change'}
                ],
                IS_ENABLE: [
                    {required: true, message: '是否启用不能为空', trigger: 'change'}
                ],
                NAME: [
                    {required: true, message: '字典名称不能为空', trigger: 'blur'}
                ],
                DICKEY: [
                    {required: true, message: '字典标识不能为空', trigger: 'blur'}
                ],
                CODE: [
                    {required: true, message: '对照码不能为空', trigger: 'blur'}
                ],
                DES: [
                    {required: true, message: '对照值不能为空', trigger: 'blur'}
                ],
                SORT_NO: [
                    {required: true, message: '排序号不能为空', trigger: 'blur'}
                ],
                NAME_EN: [
                    {required: true, message: '对照英文不能为空', trigger: 'blur'}
                ],
                NAME_TW: [
                    {required: true, message: '对照繁体不能为空', trigger: 'blur'}
                ]
            }
        },
        callObj,
        pageVue
    ;

    var DicEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'dic_edit'
            }, win);
        },
        editDic: function () {
            app.ajax({
                xurl: 'sys/dic/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '字典信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },
        addDic: function () {
            app.ajax({
                xurl: 'sys/dic/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '字典信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('dic').reloadTableCurr();
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            vueData.detail = false;
            if (obj.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        TYPE: '1',
                        IS_ENABLE: '1',
                        GEN_TYPE: '0',
                        SORT_NO: '1'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = obj.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if(obj.detail){
                    vueData.detail = true;
                }else{
                    vueData.detail = false;
                }
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['sys_dic']);
            pageVue = new Vue({
                el: '#dic_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        if(vueData.detail){
                            layer.close(layer.index);
                        }else{
                            self._closeTpl();
                        }
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addDic();
                                } else {
                                    self.editDic();
                                }
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('dic_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new DicEdit();

});